﻿using Abp.Dependency;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using NetCore.Interactive;
using System;

namespace NetCore.BackgroundWorks
{
    /// <summary>
    /// 清理临时数据的后台定时任务 (单例)
    /// 只需要继承自PeriodicBackgroundWorkerBase即可
    /// </summary>
    public class ClearTempDataBackgroundWorker : PeriodicBackgroundWorkerBase, ISingletonDependency//ITransientDependency//, 
    {
        private readonly IInteractiveAppService _interactiveAppService;//依赖注入AppService

        /// <summary>
        /// 一小时/天的 间隔时间段毫秒数
        /// </summary>
        /// <param name="timer"></param>
        public const int IntervalSeconds = 86400000;//1h:3600000;//12h 43200000//1d：86400000
        /// <summary>
        /// IInteractiveAppService interactiveAppService, 
        /// </summary>
        /// <param name="timer"></param>
        public ClearTempDataBackgroundWorker(IInteractiveAppService interactiveAppService, AbpTimer timer) : base(timer)
        {
            timer.Period = IntervalSeconds;//一小时/天执行一次 
            _interactiveAppService = interactiveAppService;
        }
        /// <summary>
        /// 每隔IntervalSeconds 时间后定时调度的方法
        /// </summary>
        protected override void DoWork()
        {
            //每周末执行1-2次
            var dayOfWeek = DateTime.Now.DayOfWeek;
            if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
            {
                Logger.Info("定时清理数据开始");
                //清理数据逻辑
                _interactiveAppService.ClearDemoData().Wait();
                Logger.Info("定时清理数据完成");
            }
        }
    }
}
